<?php

namespace app\admin\model;

use think\Model;

/**
 * Withdrawal
 * @controllerUrl 'withdrawal'
 * @property float money
 * @property int state
 * @property string order_sn
 */
class Withdrawal extends Model
{
    // 表名
    protected $name = 'withdrawal';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    protected $type = [
        'checktime' => 'timestamp:Y-m-d H:i:s',
    ];

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function getUserIdAttr($value, $row)
    {
        return !$value ? '' : $value;
    }

    public static function onBeforeDelete()
    {
        return false;
    }

    public function getMoneyAttr($value)
    {
        return bcdiv($value, 100, 2);
    }

    public function setMoneyAttr($value)
    {
        return bcmul($value, 100, 2);
    }

    public function getBeforeAttr($value)
    {
        return bcdiv($value, 100, 2);
    }

    public function setBeforeAttr($value)
    {
        return bcmul($value, 100, 2);
    }

    public function getAfterAttr($value)
    {
        return bcdiv($value, 100, 2);
    }

    public function setAfterAttr($value)
    {
        return bcmul($value, 100, 2);
    }

    public function getRatioAttr($value)
    {
        return bcdiv($value, 100, 2);
    }

    public function setRatioAttr($value)
    {
        return bcmul($value, 100, 2);
    }

    public function getReceiveAttr($value)
    {
        return bcdiv($value, 100, 2);
    }

    public function setReceiveAttr($value)
    {
        return bcmul($value, 100, 2);
    }

    public static function onBeforeInsert(Withdrawal $model)
    {
        $model->order_sn = self::getOrderSn();
    }

    public static function getOrderSn()
    {
        $sn = date('YmdHis') . rand(1000, 9999) . rand(1000, 9999);
        $has = self::where('order_sn', $sn)->count();
        if ($has > 0) {
            return self::getOrderSn();
        }
        return $sn;
    }

}